<?php

namespace App\Http\Controllers\CmsApi;

use App\Http\Model\WishDefender;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\DefenderDrawServices;


class InnerWishDefenderController extends BaseController
{
    protected $wishDefenderService;
    protected $wishDefenderModel;

    public function __construct()
    {
        $this->wishDefenderService = new DefenderDrawServices();
        $this->wishDefenderModel = new WishDefender();

    }

    /**
     * 擂主游戏设置单次抽奖消耗数量
     */
    public function wishDefenderSetNeedNum(Request $request)
    {
        $needNum = (int) $request->input('need_num', 0);
        if($needNum < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('WISHDefender:wishDefenderSetNeedNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->editWishDefenderConfig(['need_num'=>$needNum]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 设置全服扣减擂主值以及全服开启擂主值
     */
    public function wishDefenderSetStartNum(Request $request){
        $set_defender_num1 = (int) $request->post('set_defender_num1', 0);//全服一阶擂主值
        $set_defender_num2 = (int) $request->post('set_defender_num2', 0);//全服二阶擂主值
        $set_defender_num3 = (int) $request->post('set_defender_num3', 0);//全服三阶擂主值
        $set_defender_num4 = (int) $request->post('set_defender_num4', 0);//全服四阶擂主值
        $send_defender_gift1 = (int) $request->post('send_defender_gift1', 0);//全服一阶擂主值
        $send_defender_gift2 = (int) $request->post('send_defender_gift2', 0);//全服二阶擂主值
        $send_defender_gift3 = (int) $request->post('send_defender_gift3', 0);//全服三阶擂主值
        $send_defender_gift4 = (int) $request->post('send_defender_gift4', 0);//全服四阶擂主值
        $costPoolNum = (int) $request->post('costPoolNum', 0);//0点扣减擂主值
        $openMinutes = (int) $request->post('openMinutes',0);//分钟数

        //获取当前抽奖id
        $luckId = (int)Redis::get($this->wishDefenderModel->defenderLuckId);
        if ($luckId == 1000 && (!empty($set_defender_num1) || !empty($send_defender_gift1))) {
            Response::returnResult(30001, [], '进行中的阶段不能修改手数和礼物');
        }

        if ($luckId == 1001 && (!empty($set_defender_num2) || !empty($send_defender_gift2))) {
            Response::returnResult(30001, [], '进行中的阶段不能修改手数和礼物');
        }

        if ($luckId == 1002 && (!empty($set_defender_num3) || !empty($send_defender_gift3))) {
            Response::returnResult(30001, [], '进行中的阶段不能修改手数和礼物');
        }

        if ($luckId == 1003 && (!empty($set_defender_num4) || !empty($send_defender_gift4))) {
            Response::returnResult(30001, [], '进行中的阶段不能修改手数和礼物');
        }

        $data = $this->wishDefenderService->editWishDefenderConfig(['set_defender_num1'=>$set_defender_num1,'set_defender_num2'=>$set_defender_num2,
            'set_defender_num3'=>$set_defender_num3,'set_defender_num4'=>$set_defender_num4,'costPoolNum'=>$costPoolNum,'openMinutes'=>$openMinutes,'send_defender_gift1'=>$send_defender_gift1
            ,'send_defender_gift2'=>$send_defender_gift2,'send_defender_gift3'=>$send_defender_gift3,'send_defender_gift4'=>$send_defender_gift4]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏设置档位抽取次数
     */
    public function wishDefenderSetDrawNum(Request $request)
    {
        $draw_num_1 = (int) $request->input('draw_num_1', 0);
        $draw_num_2 = (int) $request->input('draw_num_2', 0);
        $draw_img_1a = getUrlPath($request->input('draw_img_1a', ''));
        $draw_img_1b = getUrlPath($request->input('draw_img_1b', ''));
        $draw_img_2a = getUrlPath($request->input('draw_img_2a', ''));
        $draw_img_2b = getUrlPath($request->input('draw_img_2b', ''));
        if($draw_num_1<1 || $draw_num_2<1 || empty($draw_img_1a) || empty($draw_img_1b) || empty($draw_img_2a) || empty($draw_img_2b)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('UNIVERSE:wishPoolSetDrawNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->editWishDefenderConfig(['draw_num_1'=>$draw_num_1,'draw_num_2'=>$draw_num_2,'draw_img_1a'=>$draw_img_1a,'draw_img_1b'=>$draw_img_1b,'draw_img_2a'=>$draw_img_2a,'draw_img_2b'=>$draw_img_2b]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏设置奖池开启/关闭
     */
    public function wishDefenderSetPoolStatus(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($poolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'UNIVERSE:wishDefenderPool';
        $ifLock = redis_lock($key, 60);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '擂主游戏更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->wishDefenderService->wishDefenderSetPoolStatus($poolId, $poolStatus);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏保存奖池配置
     */
    public function wishDefenderSavePoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolList = $request->input('pool_list');
        if(empty($poolId) || empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $poolList = json_decode($poolList, true);
        if(empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('UNIVERSE:wishDefenderSavePoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderSavePoolConfig($poolId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏新增奖池
     */
    public function wishDefenderAddPool(Request $request)
    {
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $pool_levle = (int) $request->input('pool_level',1000);//1000一阶池 1001二阶池 1002三阶池 1003四阶池
        $poolType = (int) $request->input('pool_type', 0);
        $poolType = empty($poolType) ? 1 : $poolType;
        $ifDefault = (int) $request->input('if_default', 2);
        $ifDefault = empty($ifDefault) ? 2 : $ifDefault;
        if(empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(in_array($poolType, [1]) && (empty($minNum) || empty($maxNum))) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('UNIVERSE:wishDefenderAddPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderAddPool($poolName, $minNum, $maxNum, $poolType, $ifDefault, $pool_levle);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏编辑奖池
     */
    public function wishDefenderEditPool(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $ifDefault = (int) $request->input('if_default', 0);
        $pool_level = (int) $request->input('pool_level', 1000);
        if(empty($poolId) || empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('UNIVERSE:wishDefenderEditPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderEditPool($poolId, $poolName, $minNum, $maxNum, $ifDefault, $pool_level);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏更新奖池缓存
     */
    public function wishDefenderPoolReset(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'DEFENDER:wishDefenderPool';
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '擂主游戏更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->wishDefenderService->wishDefenderPoolReset($poolId);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏奖池配置剩余数量
     */
    public function wishDefenderPoolConfigNum(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderPoolConfigNum($poolId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏新增白名单
     */
    public function wishDefenderAddWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        $poolLevel = $request->input('pool_level',1000);
        if(empty($userId) || empty($giftId) || empty($adminId) || empty($poolLevel)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('UNIVERSE:wishDefenderAddWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderAddWhite($poolLevel, $userId, $giftId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏删除白名单
     */
    public function wishDefenderDelWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('DEFENDER:wishDefenderDelWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新增擂主游戏展示礼物
     */
    public function wishDefenderAddGift(Request $request)
    {
        $giftId = (int) $request->input('gift_id', 0);
        $sort = (int) $request->input('sort', 0);
        $poolLevel = $request->input('pool_level', 1000);
        if(empty($giftId) || empty($poolLevel)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderAddGift($poolLevel, $giftId, $sort);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏删除展示礼物
     */
    public function wishDefenderDelGift(Request $request)
    {
        $id = (int) $request->input('id', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderDelGift($id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }


    /**
     * 新增擂主游戏赠送添加展示礼物
     */
    public function wishDefenderSendAddGift(Request $request)
    {
        $giftId = (int) $request->input('gift_id', 0);
        $sort = (int) $request->input('sort', 0);
        $poolLevel = $request->input('pool_level', 1000);
        if(empty($giftId) || empty($poolLevel)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderSendAddGift($poolLevel, $giftId, $sort);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏删除赠送删除展示礼物
     */
    public function wishDefenderSendDelGift(Request $request)
    {
        $id = (int) $request->input('id', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderSendDelGift($id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }


    /**
     * 擂主游戏新增个人池用户
     */
    public function wishDefenderAddPoolUser(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $adminId = $request->input('admin_id',0);
        $poolId = (int) $request->input('pool_id', 0);
        $outDiamondNum = (int) $request->input('out_diamond_num', 0);
        if(empty($userId) || empty($poolId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'DEFENDER:poolUserMake';
        if (!redis_lock($lockKey, 300)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishDefenderService->wishDefenderAddPoolUser($userId, $poolId, $outDiamondNum, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 擂主游戏删除个人池用户
     */
    public function wishDefenderDelPoolUser(Request $request)
    {
        $id = (int) $request->input('id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($id) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'WISHWHEEL:poolUserMake';
        if (!redis_lock($lockKey, 100)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishDefenderService->wishDefenderDelPoolUser($id, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //查看个人奖池剩余数
    public function wishDefenderPoolSelfConfigNum(Request $request){
        $poolId = (int) $request->input('pool_id', 0);
        $userId = (int) $request->input('user_id', 0);
        if(empty($poolId) || empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishDefenderService->wishDefenderPoolSelfConfigNum($poolId, $userId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 擂主游戏新增奖池白名单
     */
    public function wishDefenderAddPoolWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        $pool_level = $request->post('pool_level',0);
        if(empty($userId) || empty($giftId) || empty($adminId) || empty($pool_level)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('DEFENDER:wishDefenderAddPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderAddPoolWhite($userId, $giftId, $adminId, $pool_level);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏删除奖池白名单
     */
    public function wishDefenderDelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishDefenderDelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏取消投放奖池白名单
     */
    public function wishDefenderCancelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishDefenderCancelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderCancelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 擂主游戏奖池更新阀值
     */
    public function wishDefenderSetUserPoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $inProfitLoss = (int) $request->input('in_profit_loss', 0);
        $inProfitRate = (int) $request->input('in_profit_rate', 0);
        $outProfitRate = (int) $request->input('out_profit_rate', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('DEFENDER:wishDefenderSetUserPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishDefenderSetUserPoolConfig($poolId, ['in_profit_loss'=>$inProfitLoss, 'in_profit_rate'=>$inProfitRate, 'out_profit_rate'=>$outProfitRate]);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}